/**
 * Copyright (c) 2016-2020, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.addon.securityinformation.controller;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.addon.securityinformation.model.JpressAddonSecurityinformationMfdr;
import io.jpress.addon.securityinformation.service.JpressAddonSecurityinformationMfdrService;
import io.jpress.model.User;
import io.jpress.web.base.TemplateControllerBase;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;


@RequestMapping(value = "/securityinformationmfdr")
public class SecurityinformationMfdrController extends TemplateControllerBase {

    @Inject
    private JpressAddonSecurityinformationMfdrService service;

    public void index() {

        if(getLoginedUser()==null){
            redirect("/user/login");
            return;
        }

//        //防止重复编号
//        synchronized (SecurityinformationMfdrController.class) {
//            setAttr("OddNumbers", "SDR" + System.currentTimeMillis());
//        }

        String language = getPara("language");
        if (language != null && language.equals("ch")) {
            render("sdr_mfdr_ch.html");
        } else {
            render("sdr_mfdr_en.html");
        }

    }

    private String getSysYear() {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        return year;
    }

    /**
     * 提交数据
     */
    public void submit() {

        User loginedUser = getLoginedUser();
        if (loginedUser == null) {
            renderJson(Ret.fail().set("msg", "请先登录(Please login first.)"));
            return;
        }
        String[] pems = getParaValues("pems[]");
        String[] fps = getParaValues("fps[]");
        String pem = "";
        String fp = "";
        if (pems != null) {
            for (String p : pems)
                pem += (p + ",");
        }

        if (fps != null) {
            for (String f : fps)
                fp += (f + ",");
        }

        JpressAddonSecurityinformationMfdr mfdr = getModel(JpressAddonSecurityinformationMfdr.class, "mfdr");

        //----------------------------------------------------------------------------
        Record first = Db.findFirst("select max(report_number) as max from addon_s_m");
        String max = first.getStr("max");
        Integer num = 0000;
        if(max==null)
            num+=1;
        else
            num = Integer.valueOf(max.substring(max.length()-4, max.length()))+1;
        DecimalFormat df=new DecimalFormat("0000");
        String n=df.format(num);
        mfdr.setReportNumber("SDR"+getSysYear()+n);
        //----------------------------------------------------------------------------


        mfdr.setUserId(loginedUser.getId());
        mfdr.setPreventionEmergencyMeasures(pem);
        mfdr.setFaultPhenomenon(fp);
        service.save(mfdr);
        renderJson(Ret.ok());

    }

    //查询
    public void search(){

        Integer page = getParaToInt("page", 1);
        Integer pageSize = getParaToInt("pageSize", 10);
        String chapter = getPara("chapter");
        String aircraft_registration_number = getPara("aircraft_registration_number");
        Date startTime = getParaToDate("startTime");
        Date endTime = getParaToDate("endTime");
        String air_operator = getPara("air_operator");
        String report_number = getPara("report_number");
        String type = getPara("type");
        String type_operation = getPara("type_operation");

        Columns columns = Columns.create().like("chapter", "%" + chapter + "%")
                .eq("aircraft_registration_number", aircraft_registration_number)
                .ge("date_occurrence", startTime)
                .le("date_occurrence", endTime)
                .like("air_operator","%"+ air_operator +"%")
                .eq("report_number",report_number)
                .eq("type",type)
                .like("type_operation", type_operation);

        Page<JpressAddonSecurityinformationMfdr> entry =
                service.paginateByColumns(page, pageSize, columns);


        setAttr("entry",entry);

    }

}
